import { defineStore } from "pinia";
import axios from "axios";
import { computed, ref } from 'vue';
import { useRouter } from 'vue-router'
const router = useRouter()


const useLoginStore = defineStore('login', () => {
    const phone = ref('')
    const code = ref('')
    const clearText = ref(false)
    const isphonetext = ref(false)
    const iscode = ref(false)
    const isPhoneError = ref(false)
    const errorText = ref('手机号格式不正确')

    const userInfo = ref({})

    //手机号
    const phoneText = (e) => {
        clearText.value = true
        isPhoneError.value = false
    }
    const handleClearText = () => {
        clearText.value = false
        isPhoneError.value = false
        phone.value = ''
    }
    const phoneTextVerify = (e) => {  //失焦验证
        if (isPhone(e.target.value)) isphonetext.value = true
        else isphonetext.value = false
    }


    //获取验证码
    const getCode = () => {

        if (isPhone(phone.value)) {

            axios({
                method: 'post',
                url: 'http://shop-api.phpclub.org.cn/home/sms/send',
                data: {
                    mobile: phone.value,
                    scene: 1,
                },
                headers: {
                    'Content-Type': 'application/json'
                }
            }).then(res => {
                console.log(res.data.data.smsCode);
                return true
            }).catch(err => {
                console.log(err);
            })

        } else {
            console.log('验证不通过,手机号格式错误');
            return false
        }
    }
    //输入验证码
    const codeVerify = (e) => {
        if (e.target.value !== '') iscode.value = true
        else iscode.value = false
    }


    //登录
    const inputPass = computed(() => {
        // console.log(isPhone(text.value));
        // return !isPhone(text.value) 

        return !(isphonetext.value && iscode.value)
    })
    const logon = (fn) => {
        if (!isPhone(phone.value)) return
        axios({
            method: 'post',
            url: 'http://shop-api.phpclub.org.cn/home/authorize/mobileLogin',
            data: {
                mobile: phone.value,
                smsCode: code.value,
            },
            headers: {
                'Content-Type': 'application/json'
            }
        }).then(res => {
            // console.log(res.data);
            fn(res.data.data.appSessionId)
            // getUserInfo(res.data.data.appSessionId)
            // useLoginStore.userId =res.data.data.appSessionId
            // useLoginStore.mobile = res.data.data.mobile
        }).catch(err => {
            console.log(err);
        })

    }






    const isPhone = (value) => {
        let reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
        if (reg_tel.test(value)) {
            isPhoneError.value = false
            return true
        } else if (value === '') {
            errorText.value = '请输入手机号'
            isPhoneError.value = true
            return false
        } else {
            errorText.value = '手机号格式不正确'
            isPhoneError.value = true
            return false
        }
    }

    //获取用户信息
    const getUserInfo = async (userId) => {
        let res = await axios({
            url: 'http://shop-api.phpclub.org.cn/home/user/index',
            headers: {
                'Content-Type': 'application/json',
                'Authorization': 'bearer ' + userId
            }
        })
        userInfo.value = res.data.data
        localStorage.setItem('userInfo', JSON.stringify(userInfo.value))

        // router.push('/centers')
        // console.log(res.data);
        return res.data
    }

    return {
        phone,
        code,
        clearText,
        isphonetext,
        iscode,
        isPhoneError,
        errorText,
        userInfo,
        inputPass,
        phoneText,
        handleClearText,
        phoneTextVerify,
        getCode,
        codeVerify,
        logon,
    }
})
export default useLoginStore